<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/cookie.js"></script>
    <style>
        .right {
            color: green;
            font-size: 12px;
        }

        .error {
            color: red;
            font-size: 12px;
        }
    </style>
</head>

<body>
    <div class="box">
        <p>
            <label for="user">账&emsp;号:</label>
            <input id="user" type="text" placeholder="用户名/手机号/邮箱">
            <span id="user_tips"></span>
        </p>
        <p>
            <label for="pwd">密&emsp;码:</label>
            <input id="pwd" type="password">
            <span id="pwd_tips"></span>
        </p>

        <p>
            <button id="btn">注册</button>
        </p>
    </div>
</body>
<script>


    // 1. 获取元素
    var userInp = document.getElementById("user");
    var userTips = document.getElementById("user_tips");

    var pwdInp = document.getElementById("pwd");
    var pwdTips = document.getElementById("pwd_tips");

    var btn = document.getElementById("btn");

    btn.onclick = function () {

        // debugger;
        // 用户输入的
        var account = userInp.value;
        var pwd = pwdInp.value;

        var xhr = new XMLHttpRequest();

        xhr.open("post", "../php/login_account.php", true);

        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(`account=${account}&pwd=${pwd}`);

        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                console.log(xhr.responseText);
                var result = JSON.parse(xhr.responseText);
                var { status, detail, user } = result;
                if (status) {
                    // alert("登录成功");  

                    // 后端返回账号对应的用户名  前端存储
                    setCookie("lgc", user, 7);
                    location.href = "./index1.html";
                } else {
                    alert(detail);
                }

            }
        }
    }






</script>

</html>